package com.dongdong.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdong.data.Page;
import com.dongdong.data.Result;
import com.dongdong.em.ResultEmnu;
import com.dongdong.pojo.TbBrand;
import com.dongdong.service.BrandService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("brandController")
public class BrandController {

    @Reference(timeout = 3000)
    private BrandService brandService;

    @RequestMapping("toBrand")
    public String brand(){
        return "admin/brand";
    }
    @RequestMapping("brandList")
    @ResponseBody
    public Result brandList(@RequestParam(defaultValue = "1",required = false)Integer pageNum,
                            @RequestParam(defaultValue = "3")Integer pageSize){

        try{
            Page<TbBrand> tbBrandPage = brandService.brandList(pageNum, pageSize);
            return Result.respone(ResultEmnu.SUCCESS).setData(tbBrandPage);

        }catch (Exception e){
            e.printStackTrace();
            return Result.respone(ResultEmnu.ERROR).setData(e);
        }

    }
    @RequestMapping("toUpdate/{id}")
    @ResponseBody
    public Result toUpdate(@PathVariable("id") Long id){
        try{
            TbBrand tbBrand = brandService.selectById(id);
            return Result.respone(ResultEmnu.SUCCESS).setData(tbBrand);
        }catch (Exception e){
            e.printStackTrace();
            return Result.respone((ResultEmnu.ERROR)).setData(e);
        }
    }
    @RequestMapping("insertInfo")
    @ResponseBody
    public Result insertInfo(TbBrand tbBrand){
        int  i = brandService.insert(tbBrand);
        if (i > 0){
            return Result.respone(ResultEmnu.SUCCESS);
        }else {
            return Result.respone(ResultEmnu.ERROR);
        }
   }
    @RequestMapping("updateInfo")
    @ResponseBody
    public Result updateInfo(TbBrand tbBrand){
        int i = brandService.updateInfo(tbBrand);
        if (i > 0){
            return Result.respone(ResultEmnu.SUCCESS);
        }else {
            return Result.respone(ResultEmnu.ERROR);
        }
    }
    @RequestMapping("deleteAll")
    @ResponseBody
    public Result deleteAll(@RequestParam("ids[]")Long[] ids){
         int i = brandService.deleteAll(ids);
         if (i>0){
             return Result.respone(ResultEmnu.SUCCESS);
         }else {
             return Result.respone((ResultEmnu.SUCCESS));
         }
    }

}
